package com.ad.blogger.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ad.blogger.service.IBloggerUserService;
import com.ad.common.action.BaseActionSupport;
import com.ad.common.entity.User;
import com.ad.common.utils.StringUtils;

public class BloggerUserAction extends BaseActionSupport {
	private final Log logger = LogFactory.getLog(getClass());
	private IBloggerUserService bloggerUserService;
	private User user;
	private String verifyCode;

	public String getVerifyCode() {
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public String execute() throws Exception {
		return super.execute();
	}
	/**
	 * 退出登录
	 * @return
	 * @throws Exception
	 */
	public String logout() throws Exception{
		String ret = "index" ;
		this.getSessionMap().remove("b_user");
		return ret ;
	}
	public String doReg() throws Exception {
		String ret = INPUT;
		if (this.bloggerUserService.isExist(user.getUsername())) {
			addFieldError("username", getText("用户名已存在"));
		} else {
			fillRegField();
			boolean regRet = this.bloggerUserService.register(user);
			if (regRet) {
				ret = SUCCESS;
				getSessionMap().put("b_user", user);
			}
		}
		return ret;
	}

	public void doLogin() throws Exception {
		logger.info("doLogin....");
		JSONObject json = new JSONObject();
		String sessionCode = (String) this.getSessionMap().get("verifyCode");
		if (StringUtils.isEmpty(verifyCode)
				|| !verifyCode.equals(sessionCode)) {
			json.put("resultCode", "0");
			json.put("result", getText("verifyCode.error"));
		}else
		// 验证用户名
		if (StringUtils.isEmpty(user.getUsername())) {
			json.put("resultCode", "0");
			json.put("result",getText("username.required"));
		}else
		// 验证密码
		if (StringUtils.isEmpty(user.getPassword())) {
			json.put("resultCode", "0");
			json.put("result",getText("password.required"));
		}else{
		
			user = this.bloggerUserService.login(user.getUsername(),
					user.getPassword(), user.getUserType());
			if (user == null) {
				json.put("resultCode", "0");
				json.put("result",getText("login.error"));
			} else {
				getSessionMap().put("b_user", user);
				json.put("resultCode", "1");
			}
		}
		HttpServletResponse response = this.getResponse();
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter writer = this.getResponse().getWriter();
		writer.write(json.toString());
		writer.close();
	
	}
	public String userInfo() throws Exception{
		Object obj = this.getSessionMap().get("user");
		if(obj!= null){
			this.setUser((User) obj);
		}
		return "userInfo" ;
	}
	public String editUser() throws Exception{
		logger.debug("editUser...");
		User sessionUser = (User) this.getSessionMap().get("b_user");
		sessionUser.setPassword(user.getPassword());
		sessionUser.getUserInfo().setUserRealname(user.getUserInfo().getUserRealname());
		sessionUser.getUserInfo().setMobile(user.getUserInfo().getMobile());
		sessionUser.getUserInfo().setEmail(user.getUserInfo().getEmail());
		sessionUser.getUserInfo().setQq(user.getUserInfo().getQq());
		sessionUser.getUserInfo().setAlipay(user.getUserInfo().getAlipay());
		this.bloggerUserService.edit(sessionUser);
		return "userInfo" ;
	}
	
	public void fillRegField() {
		Date date = new Date();
		// 填写注册时间
		user.setRegTime(date);
		// 最后登录时间
		user.setLastLoginTime(date);
		// 最后登录ip
		user.setLastIp(this.getRequest().getRemoteAddr());
		user.setPassed(1);
		// 级别
		user.setRankId(1);
		// 默认未锁定
		user.setLocked(0);
		user.setUserType(1);
	}

	@Override
	public void validate() {

	}

	public void validateDoReg() {
		// 验证码
		if (StringUtils.isEmpty(verifyCode)
				|| !verifyCode.equals(this.getSessionMap().get("verifyCode"))) {
			addFieldError("verifyCode", getText("verify_code_error"));
		}
		// 验证用户名
		if (StringUtils.isEmpty(user.getUsername())) {
			addFieldError("username", getText("username.required"));
		}
		// 验证密码
		if (StringUtils.isEmpty(user.getPassword())) {
			addFieldError("password", getText("password.required"));
		}

		// 验证重复密码
		if (!user.getPassword().equals(user.getRpassword())) {
			addFieldError("rpassword", getText("password.not_match"));
		}
		// 验证微博主姓名
		if (StringUtils.isEmpty(user.getUserInfo().getUserRealname())) {
			addFieldError("realname", getText("realname.required"));
		}
		// 验证手机号
		if (StringUtils.isEmpty(user.getUserInfo().getMobile())) {
			addFieldError("mobile", getText("mobile.required"));
		}
		// 验证微博主邮箱
		if (StringUtils.isEmpty(user.getUserInfo().getEmail())) {
			addFieldError("email", getText("email.required"));
		}
		// //QQ
		// if(StringUtils.isEmpty(user.getUsername())){
		// addFieldError("username",getText("username.required"));
		// }
		// 支付宝账号
		if (StringUtils.isEmpty(user.getUserInfo().getAlipay())) {
			addFieldError("alipay", getText("alipay.required"));
		}
	}
	public void validateEditUser(){
		Enumeration  names = this.getRequest().getParameterNames();
		String tmp = null;
		while(names.hasMoreElements()){
			tmp = (String) names.nextElement();
			logger.debug(tmp+"======"+this.getRequest().getParameter(tmp));
		}
		// 验证码
		if (StringUtils.isEmpty(verifyCode)
				|| !verifyCode.equals(this.getSessionMap().get("verifyCode"))) {
			addFieldError("verifyCode", getText("verify_code_error"));
		}
		// 验证用户名
		if (StringUtils.isEmpty(user.getUsername())) {
			addFieldError("username", getText("username.required"));
		}
		// 验证密码
		if (StringUtils.isEmpty(user.getPassword())) {
			addFieldError("password", getText("password.required"));
		}

		// 验证重复密码
		if (!user.getPassword().equals(user.getRpassword())) {
			addFieldError("rpassword", getText("password.not_match"));
		}
		// 验证微博主姓名
		if (StringUtils.isEmpty(user.getUserInfo().getUserRealname())) {
			addFieldError("realname", getText("realname.required"));
		}
		// 验证手机号
		if (StringUtils.isEmpty(user.getUserInfo().getMobile())) {
			addFieldError("mobile", getText("mobile.required"));
		}
		// 验证微博主邮箱
		if (StringUtils.isEmpty(user.getUserInfo().getEmail())) {
			addFieldError("email", getText("email.required"));
		}
	
		// 支付宝账号
		if (StringUtils.isEmpty(user.getUserInfo().getAlipay())) {
			addFieldError("alipay", getText("alipay.required"));
		}
	}
	public IBloggerUserService getBloggerUserService() {
		return bloggerUserService;
	}

	public void setBloggerUserService(IBloggerUserService bloggerUserService) {
		this.bloggerUserService = bloggerUserService;
	}

}
